package top.eggcode.system.modules.user.control;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import top.eggcode.common.base.BaseController;
import top.eggcode.common.lang.BeanUnit;
import top.eggcode.common.model.ListPage;
import top.eggcode.common.model.ResponseResult;
import top.eggcode.common.mvc.Responser;
import top.eggcode.plugins.dict.Translate;
import top.eggcode.plugins.query.CriteriaBuilder;
import top.eggcode.system.modules.user.model.*;
import top.eggcode.system.modules.user.service.PowerServiceImpl;
import top.eggcode.system.modules.user.tool.MenuTool;
import top.eggcode.system.modules.user.tool.PowerTreeTool;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * Title:
 * Description: TODO
 * Date: 2021/5/10 9:54
 *
 * @author JiaQi Ding
 * @version 1.0
 */
@Api(tags = "操作权限")
@RequiredArgsConstructor
@RequestMapping("/api/power")
@RestController
public class PowerController extends BaseController<PowerDTO, PowerQuery> {

    private final PowerServiceImpl selfService;

    @Resource
    private CriteriaBuilder builder;

    @Override
    @GetMapping("/{id}")
    public ResponseResult<?> get(@PathVariable String id) {
        return Responser.successful(
                BeanUnit.transfer(selfService.get(id).get(), PowerView.class)
        );
    }

    @Override
    @ApiOperation("新增")
    @PostMapping("/add")
    public ResponseResult<?> add(@RequestBody PowerDTO form) {
        selfService.put(BeanUnit.transfer(form, PowerEntity.class));
        return Responser.successful();

    }

    @Override
    @ApiOperation("更新")
    @PatchMapping("/update")
    public ResponseResult<?> update(@RequestBody PowerDTO form) {
        selfService.update(form.convert());
        return Responser.successful();
    }

    @Translate
    @ApiOperation("列表分页")
    @GetMapping("/list/page")
    @Override
    public ResponseResult<?> listPage(PowerQuery query) {
        ListPage<PowerEntity> page = selfService.page(query);
        List<PowerEntity> doList = page.getList();
        List<PowerView> voList = new LinkedList<>();
        doList.stream()
                .map(PowerView::build)
                .forEach(voList::add);
        ListPage<PowerView> view = new ListPage<PowerView>()
                .setNumber(page.getNumber())
                .setSize(page.getSize())
                .setTotal(page.getTotal())
                .setList(PowerTreeTool.generateRootNode(voList));
        return Responser.successful(view);
    }

    @ApiOperation("生成菜单树")
    @GetMapping("/menu/tree")
    public ResponseResult<?> menuList() {
        List<MenuNodeView> menuList = new LinkedList<>();
        selfService.queryMenuList()
                .stream()
                .map(MenuNodeView::build)
                .forEach(menuList::add);
        return Responser.successful(MenuTool.generateTree(menuList));
    }

    @Override
    @ApiOperation("删除")
    @DeleteMapping("/{id}/delete")
    public ResponseResult<?> delete(@PathVariable String id) {
        selfService.deleteById(id);
        return Responser.successful();
    }


}
